package org.kingkong.framework.resource;

import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.kingkong.framework.annotation.SessionBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.ResourcePatternResolver;

@SessionBean
public class Language extends HashMap<String, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4938657116172206495L;

	private static final String sep = "/";

	private static final String languagePath = new StringBuilder()
			.append("classpath*:").append("org").append(sep).append("kingkong")
			.append(sep).append("languages").append(sep)
			.append("*_language.properties").toString();

	private static final ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

	public Language() {

	}

	@PostConstruct
	private void initialisation() throws Exception {
		Resource[] resources = resourcePatternResolver
				.getResources(languagePath);

		for (final Resource resource : resources) {
			Properties properties = PropertiesLoaderUtils
					.loadProperties(resource);
			Set<Object> keys = properties.keySet();
			for (Object object : keys) {
				if (!this.containsKey(object)) {
					this.put((String) object, (String) properties.get(object));
				}
			}
		}
	}

	@Override
	public String get(Object key) {
		if (!super.containsKey(key)) {
			return "???" + (String) key + "???";
		}
		return super.get(key);
	}
}
